﻿using BlueCat.Models;
using BlueCat.Views;
using DramaEnglish.WPF.ViewModels;
using HandyControl.Controls;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Open.Infrastructure.Http;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;

namespace BlueCat.ViewModels
{
    public class StudyListViewModel : ViewModelBase
    {
        #region Fields
        private readonly Logger<StudyListViewModel> logger;
        private readonly string _token;
        private Study CurrentStudy = new();
        HttpClientBase HttpClient;

        private ObservableCollection<Study> studyList = new ObservableCollection<Study>();

        public ObservableCollection<Study> StudyList
        {
            get { return studyList; }
            set { SetProperty(ref studyList, value); }
        }
        #endregion

        #region Properties

        #endregion

        #region Constructors
        public StudyListViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator ea, HttpClientBase client, Logger<StudyListViewModel> logger)
           : base(regionManager, dialogService, ea)
        {
            this.logger = logger;
            HttpClient = client;
            Init();
        }
        #endregion

        #region Cammands

        public DelegateCommand<DataGrid> SelectStudyCommand => new((a) =>
        {
            CurrentStudy = a.SelectedItem as Study;
            SwitchView("MainShell", typeof(DcmViewer));
        });
        #endregion

        #region Functions Methods
        private async void Init()
        {
            try
            {
                //Nlog.config 复制到输出目录 
                logger.LogDebug("Hello");
                var resutl = await HttpClient.GetStringAsync("/api//studies?limit=25&offset=0&fuzzymatching=true&includefield=00081030%2C00080060");
                StudyList = JsonConvert.DeserializeObject<ObservableCollection<Study>>(resutl);
            }
            catch (Exception ex)
            {
                Growl.Error(ex.Message, _token);
            }
        }
        #endregion 
    }
}
